package com.FH.applyForm.officeServies.notice_type;

import com.FH.util.AccessDB;
import com.FH.util.ResultDTO;
import com.FH.util.warnSrc;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

import javax.ejb.Stateless;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * Created by Administrator on 2016/10/25.
 */
@Stateless
public class NoticeTypeDAOImpl extends AccessDB implements NoticeTypeDAO {

    /**
     * 查询公告类型
     * @param
     * @param page
     * @param size
     * @return
     */
    @Override
    public ResultDTO getNoticeTypeInfo(String search, String page, String size) {
        ResultDTO resultDTO = new ResultDTO();

        try {
            JSONObject json = new JSONObject(search);
            String existName = "0";

            String sqlexit = "select count(1) from notice_type  where 1=1 ";

            String sql_w = "";//查询条件sql组合

            String sql_page = "";//分页sql组合
            Integer page_ok = Integer.parseInt(page);
            Integer size_ok = Integer.parseInt(page);
            if (page.equals("1")) {
                sql_page = " limit 0," + size + "";
            } else {
                sql_page = " limit " + ((page_ok - 1) * size_ok + 1) + "," + size + "";
            }

            if (json.has("id")) {
                sql_w += " AND id ='" + json.getString("id") + "'";
            }
            if (json.has("noticetype_name")) {
                sql_w += " AND noticetype_name ='" + json.getString("noticetype_name") + "'";
            }

            String sql_ok = sqlexit + sql_w;//组合好的sql语句
            existName = getReportingToOne(sql_ok);//查询此数据是否存在

            List<String[]> seresult = new ArrayList<String[]>();

            List<NoticeTypeShowDTO> result = new ArrayList<NoticeTypeShowDTO>();

            if (!existName.equals("0")) {
                String sql = "SELECT id,noticetype_name FROM notice_type WHERE 1=1 " + sql_w + sql_page;
                seresult = this.getReportingTo(sql);

                //构建返回对象
                for (int i = 0; i < seresult.size(); i++) {

                    NoticeTypeShowDTO noticeTypeShowDTO = new NoticeTypeShowDTO();
                    String id = seresult.get(i)[1];
                    noticeTypeShowDTO.setId(id);
                    noticeTypeShowDTO.setNoticetype_name(seresult.get(i)[2]);

                    result.add(noticeTypeShowDTO);

                }
            }
            if (seresult.size() != 0) {
                resultDTO.setValues(result);
                resultDTO.setStatus("90000");
                resultDTO.setDescritpion(warnSrc.get_success);
            } else {
                resultDTO.setStatus("90001");
                resultDTO.setDescritpion(warnSrc.get_fail);
            }

            resultDTO.setValues(result);

        } catch (JSONException e) {
            e.printStackTrace();
        }

        return resultDTO;

    }

    /**
     * 添加公告类型
     * @param
     * @return
     */
    @Override
    public ResultDTO addNoticeTypeInfo(NoticeTypePullDTO noticeTypePullDTO) {
        int resultls=0;
        ResultDTO resultDTO=new ResultDTO();
        String uuid = UUID.randomUUID().toString().trim().replaceAll("-", "");

        String noticetype_name=noticeTypePullDTO.getNoticetype_name();
        String sql="select * from notice_type where noticetype_name='"+noticetype_name+"'";
        String  sum=getReportingToOne(sql);
        if(!sum.equals("")){

            resultDTO.setStatus("90001");
            resultDTO.setDescritpion("信息已存在，请重新分配");
            return resultDTO;
        }
        String sql1="";
        sql1="insert into notice_type (id,noticetype_name)" +
                "values('"+uuid+"','"+noticeTypePullDTO.getNoticetype_name()+"')";

        resultls=addReportingTo(sql1);
        if(resultls!=0){
            resultDTO.setStatus("90000");
            resultDTO.setDescritpion(warnSrc.add_success);
        }else{
            resultDTO.setStatus("90001");
            resultDTO.setDescritpion(warnSrc.add_fail);
        }
        return resultDTO;
    }


    /**
     * 修改公告类型
     * @param noticeTypePullDTO
     * @return
     */
    @Override
    public ResultDTO updateNoticeTypeInfo(NoticeTypePullDTO noticeTypePullDTO) {
        ResultDTO resultDTO = new ResultDTO();

        String QUERY = "";

        //更新sql语句
        QUERY += "update notice_type set ";
        if (null != noticeTypePullDTO.getNoticetype_name() && !"".equals(noticeTypePullDTO.getNoticetype_name())) {
            QUERY += " noticetype_name = '" + noticeTypePullDTO.getNoticetype_name() + "'";
        }

        QUERY += " where id = '" + noticeTypePullDTO.getId() + "'";


        int result = this.addReportingTo(QUERY);

        // 判断SQl是否执行成功
        if (result != 0) {

            resultDTO.setStatus("90000");
            resultDTO.setDescritpion(warnSrc.update_success);
        } else {
            resultDTO.setStatus("90001");
            resultDTO.setDescritpion(warnSrc.update_fail);
        }
        return resultDTO;


    }

    /**
     * 删除公告类型
     * @param noticeTypePullDTO
     * @return
     */
    @Override
    public ResultDTO deleteNoticeTypeInfo(NoticeTypePullDTO noticeTypePullDTO) {
        String sql="select count(1) from notice_type WHERE noticetype_name='"+noticeTypePullDTO.getNoticetype_name()+"'";
        String existadvance= getReportingToOne(sql);

        int result = 0;

        ResultDTO resultDTO = new ResultDTO();
        // 删除SQL语句
        if(!existadvance.equals("0")){
            String QUERY = "";
            QUERY += "delete from notice_type  where noticetype_name = '"+noticeTypePullDTO.getNoticetype_name()+"'";

            result = this.addReportingTo(QUERY);
        }

        // 判断SQl是否执行成功
        if (result!= 0) {
            resultDTO.setStatus("90000");
            resultDTO.setDescritpion(warnSrc.delete_success);
        }else {
            resultDTO.setStatus("90001");
            resultDTO.setDescritpion(warnSrc.delete_fail);
        }
        return resultDTO;
    }
}
